<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主机名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入主机名称"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
       <el-form-item label="主机地址" prop="hostname">
        <el-input
          v-model="queryParams.hostname"
          placeholder="请输入主机地址"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
       <el-form-item label="创建时间">
        <el-date-picker
          v-model="queryParams.params"
          style="width: 308px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增服务器</el-button>
      </el-col>
    
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="number" width="60" />
      <el-table-column label="主机名称" align="center" prop="name" />
      <el-table-column label="主机地址" align="center" prop="hostname" />
      <el-table-column label="主机登录名" align="center" prop="username" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主机名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入主机名称" />
        </el-form-item>
        <el-form-item label="主机地址" prop="hostname">
          <el-input v-model="form.hostname" placeholder="请输入主机地址" />
        </el-form-item>
        <el-form-item label="登录名" prop="username">
          <el-input v-model="form.username"  placeholder="请输入登录名"/>
        </el-form-item>
        <el-form-item v-if="isUp" label="主机密码" prop="passwd">
          <el-input v-model="form.passwd" placeholder="请输入主机密码" type="password" show-password/>
        </el-form-item>
       </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs } from 'vue';
import { list, detail, del, add, put } from '@/api/script/host';
const { proxy } = getCurrentInstance();
const loading = ref(true);
const ids = ref('');
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const postList = ref([]);
const open = ref(false);
const title = ref("");
const isUp = ref(true);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  hostname: undefined,
  params: []
});

const form = reactive({
  name: undefined,
  username: undefined,
  hostname: undefined,
  port: 22,
  passwd: undefined
});

const rules = reactive({
  name: [
    { required: true, message: "名称不能为空", trigger: "blur" }
  ],
  hostname: [
    { required: true, message: "主机地址不能为空", trigger: "blur" }
  ],
  username: [
    { required: true, message: "登录名不能为空", trigger: "blur" }
  ],
  passwd: [
    { required: true, message: "登录密码不能为空", trigger: "blur" }
  ]
});

function getList() {
  loading.value = true;
  queryParams.startTime = queryParams.params[0];
  queryParams.endTime = queryParams.params[1];
  list(queryParams).then(response => {
    postList.value = response.rows.map((item, index) => { item.number = index + 1; return item });
    total.value = response.total;
    loading.value = false;
  });
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.name = undefined;
  form.username = undefined;
  form.hostname = undefined;
  form.port = 22;
  form.passwd = undefined;
  proxy.resetForm("formRef");
}

function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

function resetQuery() {
  queryParams.params = [];
  proxy.resetForm("queryForm");
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id).join(',');
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加服务器";
}

function handleUpdate(row) {
  reset();
  isUp.value = false;
  const id = row.id || ids.value;
  detail(id).then(response => {
    form.id = id;
    form.name = response.data.name;
    form.username = response.data.username;
    form.hostname = response.data.hostname;
    form.passwd = response.data.passwd;
    open.value = true;
    title.value = "修改服务器";
  });
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.id != undefined) {
        put(form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        add(form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  const idsToDelete = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除主机编号为"' + idsToDelete + '"的数据项？').then(function() {
    return del(idsToDelete);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleExport() {
  proxy.download('system/post/export', {
    ...queryParams
  }, `post_${new Date().getTime()}.xlsx`)
}

getList();
</script>
